﻿using System;
using System.Collections.Generic;
using System.Linq;
using DDDSample.Domain.Cargo;
using DDDSample.Domain.Location;
using DDDSample.Domain.NHibernate.Mappings;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NHibernate;
using NHibernate.Linq;
using NHibernate.Tool.hbm2ddl;

namespace DDDSample.Test
{
    [TestClass]
    public class TestBase
    {
        private const string DbFile = @"ddd.sqlite";
        private const string HbmPath = @"..\..\Hbms";
        private const string DbScriptFile = @"db.sql";

        protected static ISessionFactory SessionFactory;

        protected static void BuildSessionFactory(bool buildTables = false)
        {
            SessionFactory = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.ShowSql().UsingFile(DbFile))
                .Mappings(m =>
                {
                    m.FluentMappings.AddFromAssemblyOf<CargoMap>();
                    m.FluentMappings.ExportTo(HbmPath);

                }).ExposeConfiguration(c =>
                {
                    c.SetInterceptor(new SqlInterceptor());
                    if (buildTables)
                    {
                        var schema = new SchemaExport(c);
                        schema.SetOutputFile(DbScriptFile).Execute(true, true, false);
                    }
                }).BuildSessionFactory();
        }

        [TestMethod]
        public void NHibernateCfgSuccess()
        {
            BuildSessionFactory(true);
            using (var session = SessionFactory.OpenSession())
            {
                var cargos = session.Query<Cargo>().ToList();
                Console.WriteLine(cargos.Count);
            }
        }

        [TestMethod]
        public void InitLocation()
        {
            BuildSessionFactory(true);
            using (var session = SessionFactory.OpenSession())
            {
                var locations = new List<Location>
                {
                    new Location(new UnLocode("CNHKG"), "Hongkong"),
                    new Location(new UnLocode("AUMEL"), "Melbourne"),
                    new Location(new UnLocode("SESTO"), "Stockholm"),
                    new Location(new UnLocode("FIHEL"), "Helsinki"),
                    new Location(new UnLocode("USCHI"), "Chicago"),
                    new Location(new UnLocode("JNTKO"), "Tokyo"),
                    new Location(new UnLocode("DEHAM"), "Hamburg"),
                    new Location(new UnLocode("CNSHA"), "Shanghai"),
                    new Location(new UnLocode("NLRTM"), "Rotterdam"),
                    new Location(new UnLocode("SEGOT"), "Göteborg"),
                    new Location(new UnLocode("CNHGH"), "Hangzhou"),
                    new Location(new UnLocode("USNYC"), "New York"),
                    new Location(new UnLocode("USDAL"), "Dallas"),
                };
                foreach (var item in locations)
                {
                    session.Save(item);
                }
                session.Flush();
            }
        }
    }
}